package com.anxin.service.impl;

import com.anxin.entity.Appointment;
import com.anxin.mapper.AppointmentMapper;
import com.anxin.service.AppointmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.util.List;

@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createAppointment(Appointment appointment) {
        this.save(appointment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelAppointment(String appointmentId, String reason) {
        Appointment appointment = new Appointment();
        appointment.setId(appointmentId);
        appointment.setStatus(3); // 已取消
        appointment.setCancelReason(reason);
        this.updateById(appointment);
    }

    @Override
    public List<Appointment> getPatientAppointments(String patientId) {
        return this.list(new LambdaQueryWrapper<Appointment>()
                .eq(Appointment::getPatientId, patientId)
                .orderByDesc(Appointment::getCreateTime));
    }

    @Override
    public List<Appointment> getDoctorAppointments(String doctorId, LocalDate date) {
        return this.list(new LambdaQueryWrapper<Appointment>()
                .eq(Appointment::getDoctorId, doctorId)
                .eq(Appointment::getAppointmentDate, date)
                .orderByAsc(Appointment::getVisitNumber));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAppointmentStatus(String appointmentId, Integer status) {
        Appointment appointment = new Appointment();
        appointment.setId(appointmentId);
        appointment.setStatus(status);
        this.updateById(appointment);
    }
} 